//
//  DrawGround.m
//  绘图板
//
//  Created by hyrMac on 15/8/5.
//  Copyright (c) 2015年 hyrMac. All rights reserved.
//

#import "DrawGround.h"
//#import "MyButton.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

//typedef void (^Type) (void);

@implementation DrawGround

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.paintColor = [UIColor blackColor];
        self.lineWidth = 5.0f;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    if ((!self.points) || (self.points.count < 2)) {
        return;
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapRound);
    
//    //设置画笔粗细
//    CGContextSetLineWidth(context, 5.0f);
//    //设置画笔颜色
//    CGContextSetStrokeColorWithColor(context, _paintColor.CGColor);
    
    //画以前的轨迹
    for (int j = 0 ; j < [_allPoints count]; j++) {
        
        NSMutableDictionary *dictTmp = _allPoints[j];
        NSMutableArray *pointsTmp = [dictTmp objectForKey:@"pointLocations"];
        
        //设置画笔颜色
        UIColor *currentColor = [dictTmp objectForKey:@"paintColor"];
        CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
        
        //设置画笔粗细
        float currentLineWidth = [[dictTmp objectForKey:@"lineWidth"] floatValue];
        CGContextSetLineWidth(context, currentLineWidth);
//        for (int i = 0;i < [pointsTmp count]-1;i++) {
//            
//            CGPoint point1 = [[pointsTmp objectAtIndex:i] CGPointValue];
//            CGPoint point2 = [[pointsTmp objectAtIndex:(i+1)] CGPointValue];
//            CGContextMoveToPoint(context, point1.x, point1.y);
//            CGContextAddLineToPoint(context, point2.x, point2.y);
//            CGContextStrokePath(context);
//        }
        CGPoint p[pointsTmp.count];
        for (int i = 0; i < pointsTmp.count; i++) {
            p[i] = [pointsTmp[i] CGPointValue];
        }
        CGContextAddLines(context, p , pointsTmp.count);
        CGContextDrawPath(context, kCGPathStroke);
    }
    
    //画这次
    CGContextSetStrokeColorWithColor(context, _paintColor.CGColor);
    CGContextSetLineWidth(context, self.lineWidth);
//    for (int i = 0; i < [self.points count]-1; i++) {
//        
//        CGPoint point1 = [[self.points objectAtIndex:i] CGPointValue];
//        CGPoint point2 = [[self.points objectAtIndex:(i+1)] CGPointValue];
//        CGContextMoveToPoint(context, point1.x, point1.y);
//        CGContextAddLineToPoint(context, point2.x, point2.y);
//        CGContextStrokePath(context);
//    }
    CGPoint p[self.points.count];
    for (int i = 0; i < self.points.count; i++) {
        p[i] = [self.points[i] CGPointValue];
    }
    CGContextAddLines(context, p , self.points.count);
    CGContextDrawPath(context, kCGPathStroke);

}

// 创建一个array，并且记录初始ponit
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
    self.points = [NSMutableArray array];
    CGPoint pt = [[touches anyObject] locationInView:self];
    [self.points addObject:[NSValue valueWithCGPoint:pt]];
    
    // 把点的位置和颜色纪录到字典中
    _dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.points,@"pointLocations",self.paintColor,@"paintColor",[NSNumber numberWithFloat:self.lineWidth],@"lineWidth", nil];
}

// 移动过程中记录这些points
// 调用setNeedsDisplay，会触发drawRect方法的调用
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint pt = [[touches anyObject] locationInView:self];
    [self.points addObject:[NSValue valueWithCGPoint:pt]];
    
    [_dict setObject:self.points forKey:@"pointLocations"];
    
    [self setNeedsDisplay];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//    NSMutableArray *pointsTmp = [[NSMutableArray alloc] initWithArray:self.points];
    NSMutableDictionary *pointsTmp = [[NSMutableDictionary alloc] initWithDictionary:_dict];
    if (self.allPoints == nil) {
        self.allPoints = [NSMutableArray arrayWithObject:pointsTmp];
    }else {
        [self.allPoints addObject:pointsTmp];
    }
}

@end
